/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package imessenger;

import java.util.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import ymsg.network.*;
import ymsg.support.*;
import ymsg.test.*;
/**
 *
 * @author Shelley Jane C. Co
 */
public class IMClient 
{
    private Session ymSession = null;
    private IMcontactsGui hostContacts;
    private IMListener imListener;
    private String sessionHost = "";
    public boolean isLoggedIn = false;
    
    public IMClient()
    {
        ymSession = new Session();
        imListener = new IMListener(this);
        ymSession.addSessionListener(imListener);
    }
    
    public IMClient(HTTPConnectionHandler proxy)
    {
        ymSession = new Session(proxy);
        System.out.println(proxy.toString()+" "+proxy.PROXY_HOST);
        imListener = new IMListener(this);
        ymSession.addSessionListener(imListener);
    }
    
    public void login(String uname, char[] cw)
    {
        String pword = String.valueOf(cw);
        if (this.isLoggedIn == false)
        {
            try 
            {
                System.out.println("Username: " + uname);
                System.out.println("Password: " + pword);
                System.setProperty("ymsg.network.loginTimeout", "-1");
                
                getYmSession().login(uname, pword);
                
                if(getYmSession().getSessionStatus() == getYmSession().MESSAGING)
                {   
                    // We might want to save the port somewhere for next login attempt
                    System.out.println("Login Success!!!");
                    this.isLoggedIn = true;
                    this.sessionHost = uname;
                    ymSession.setStatus("thesis testing", true);
                    //ymSession.sendMessage("armandloxington", "hei!!! thesis test..");
                    //System.out.println("Message Sent Success!!!");
                    //ymSession.logout();
                }
                else
                {   // Failed to log in - reset so we can re-use the same object
                    //ymSession.reset();
                    this.isLoggedIn = false;
                }
            }
            catch (IOException io)
            {
                System.out.println("[IO Error] : "+io.getMessage());
                io.printStackTrace();
            }
            catch (LoginRefusedException ex)
            {
                this.isLoggedIn = false;
                System.out.println("[Refused Login Error] : "+ex.getMessage());
                //ex.printStackTrace();
                ymSession.reset();
                IMloginGui loginGui = new IMloginGui();
                loginGui.setVisible(true);
            }
            catch (Exception ex) {
                System.out.println("[Login Error] : "+ex.getMessage());
                ex.printStackTrace();
            }
        }
    }
    
    public void logout()
    {
        try 
        {
            if (this.isLoggedIn)
            {
                this.getYmSession().logout();
                this.isLoggedIn = false;
            }
        } catch (IOException io)
        {
            System.out.println("[IO Error] : "+io.getMessage());
            io.printStackTrace();
        }
        catch (Exception ex) {
            System.out.println("[Login Error] : "+ex.getMessage());
            ex.printStackTrace();
        }
    }
    
    public void runIM()
    {
        IMcontactsGui conGui = new IMcontactsGui(this);
        this.hostContacts = conGui;
        conGui.setVisible(true);
        conGui.setTitle(this.sessionHost);
        //conGui.displayContacts(getAllContacts());
    }
    
    public Vector getOnlineContacts()
    {
        YahooGroup[] yg = getYmSession().getGroups();
        Vector <String> ret = new Vector();
        
	for(int i=0;i<yg.length;i++)
	{
            Vector <YahooUser> members = yg[i].getMembers();
            ret.addElement("["+yg[i].getName()+"]");
            
            if(!members.isEmpty())
            {
                Iterator it = members.iterator();
                while (it.hasNext())
                {
                    YahooUser yu = (YahooUser)it.next();
                    //System.out.println("\t"+yu.getId());
                    if (yu.isLoggedIn())
                        ret.addElement(yu.getId());
                }
            }
	}        
        return ret;
    }
    
    public Vector getAllContacts()
    {
        YahooGroup[] yg = getYmSession().getGroups();
        Vector <String> ret = new Vector();
        
	for(int i=0;i<yg.length;i++)
	{
            //System.out.println(yg[i].getName());
            Vector <YahooUser> members = yg[i].getMembers();
            ret.addElement("["+yg[i].getName()+"]");
            
            if(!members.isEmpty())
            {
                Iterator it = members.iterator();
                while (it.hasNext())
                {
                    YahooUser yu = (YahooUser)it.next();
                    //System.out.println("\t"+yu.getId());
                    ret.addElement(yu.getId());
                }
            }
	}        
        return ret;
    }
    
    public String getUserStatus(YahooUser yu)
    {
        String status = null;
        
        if (yu.getCustomStatusMessage() == null)
        {
            int temp = (int)yu.getStatus();
            switch (temp)
            {
                case (int)StatusConstants.STATUS_AVAILABLE : status = StatusConstants.STATUS_AVAILABLE_STR;
                    break;
                case (int)StatusConstants.STATUS_BRB : status = StatusConstants.STATUS_BRB_STR;
                    break;
                case (int)StatusConstants.STATUS_BUSY : status = StatusConstants.STATUS_BUSY_STR;
                    break;
                case (int)StatusConstants.STATUS_IDLE : status = StatusConstants.STATUS_IDLE_STR;
                    break;
                case (int)StatusConstants.STATUS_STEPPEDOUT : status = StatusConstants.STATUS_STEPPEDOUT_STR;
                    break;
                case (int)StatusConstants.STATUS_NOTATDESK : status = StatusConstants.STATUS_NOTATDESK_STR;
                    break;
                case (int)StatusConstants.STATUS_NOTATHOME : status = StatusConstants.STATUS_NOTATHOME_STR;
                    break;
                case (int)StatusConstants.STATUS_NOTINOFFICE : status = StatusConstants.STATUS_NOTINOFFICE_STR;
                    break;
                case (int)StatusConstants.STATUS_ONPHONE : status = StatusConstants.STATUS_ONPHONE_STR;
                    break;
                case (int)StatusConstants.STATUS_OUTTOLUNCH : status = StatusConstants.STATUS_OUTTOLUNCH_STR;
                    break;
                case (int)StatusConstants.STATUS_ONVACATION : status = StatusConstants.STATUS_ONVACATION_STR;
                    break;
                default : break;
            }
        }
        else
            status = yu.getCustomStatusMessage();
        
        return status;
    }

    public Session getYmSession() {
        return ymSession;
    }

    public String getSessionHost() {
        return sessionHost;
    }

    public IMcontactsGui getHostContacts() {
        return hostContacts;
    }

    public IMListener getImListener() {
        return imListener;
    }

}
